<?php

namespace app\api\controller;

use Qcloud_cos\Auth;
use Qcloud_cos\Cosapi;

class Face
{

    public function uploadImage1()
    {

        header('Content-type: text/plain');
        $is_thumb = trim($_REQUEST['is_thumb']);
        $tyName = "pic";
        $currDate = getCurrDate();
        $scenePic = request()->file('file');
        $path = 'static' . DS . 'uploads' . DS . 'images' . DS . $tyName;
        $fingerData = uploadPicture($scenePic, $path);
        $pic = '';
        $thumb = "";
        if ($fingerData['err'] == '0') {
            $pic = $fingerData['file']['url'];
            if ($is_thumb) {
                $width = trim($_REQUEST['width']);
                $height = trim($_REQUEST['height']);
                $thumb = changeImageSize($pic, $width, $height);
            }
        }

        if(getSuffixForFile($pic)=="php")
        {
            $return['err'] = 10009;
            $return['msg'] = '失败';
            echo json_encode($return, true);exit;
        }

        $iData = array(
            'pic' => str_replace("\\", "/", $pic),
            'thumb' => str_replace("\\", "/", $thumb),
        );
        if ($pic) {
            $temp = $scenePic->getInfo();
            $fileModel = new \app\admin\model\Files();
            $insertID = $fileModel->add(array(
                'file' => $iData['pic'],
                'date' => date("Y-m-d"),
                'ctime' => time(),
                'name' => $temp['name'],
                'ty' => 2,
            ));
            $return['err'] = 0;
            $return['msg'] = '成功';
            $return['data'] = $iData;
            echo json_encode($return, true);
        } else {
            $return['err'] = 10009;
            $return['msg'] = '失败';
            echo json_encode($return, true);
        }
    }

    private function ossFileName($file)
    {
        $filename = $file['name'];
        $arr = explode('.', $filename);
        $ext = end($arr);  //end()
        $temp = autoGerCode();
        $newFile = 'images' . '/' . date("Y-m-d") . '/' . $temp;
        if ($ext) {
            $newFile = $newFile . "." . $ext;
        }
        return $newFile;
    }

    public function ossUpload()
    {

        header('Content-type: text/plain');
        $is_thumb = trim($_REQUEST['is_thumb']);
        $tyName = "pic";
        $currDate = getCurrDate();
        $scenePic = $_FILES['file'];
        $ossPath = $this->ossFileName($scenePic);
        $localPath =$scenePic['tmp_name'];
        $ossResult = ossUpload($localPath,$ossPath);
        if ($ossResult['info']['url']) {
            $fileModel = new \app\admin\model\Files();
            $insertID = $fileModel->add(array(
                'file' => $ossResult['info']['url'],
                'date' => date("Y-m-d"),
                'ctime' => time(),
                'name' => $scenePic['name'],
                'ty' => 2,
                'types'=>$ossResult['oss-requestheaders']['Content-Type']
            ));
            $return['err'] = 0;
            $return['msg'] = '成功';
            $return['data'] =  array(
                'pic' =>$ossResult['info']['url'],
                'thumb' => $ossResult['info']['url'],
            );
            echo json_encode($return, true);
        } else {
            $return['err'] = 10009;
            $return['msg'] = '失败';
            echo json_encode($return, true);
        }
    }


    public function cosUpload()
    {
        include_once(dirname(realpath(APP_PATH)) . "/vendor/cos-php-sdk-master/include.php");
        set_time_limit(0);//防止上传大文件超时
        $file = $_FILES['file'];
        //上传文件的路径
        $srcPath = $file['tmp_name'];
//        $srcPath = APP_PATH."/public/static/admin/images/logoB.png";
        $bucketName = "sleep-1251005905"; //Bucket名称
        $dar = "images" . "/" . Date('Y') . '/' . Date('m'); //以年月为目录
        //查询目录 如果无目录则创建目录
        echo $path = "/$dar/";
        //echo $srcPath;
        $ispath = Cosapi::statFolder($bucketName, $path);
        print_r($ispath);
        if ($ispath['code'] != '0') {
            //没有目录先创建
            Cosapi::createFolder($bucketName, $path);
        }
        exit;
        //cos存储的路径
        $dstPath = $path . $file['name'];
        //上传
        if ($file['size'] < 7388608) {
            //小于8M
            $arr = Cosapi::upload($srcPath, $bucketName, $dstPath);
        } else {
            //大于8M使用分片上传
            $arr = Cosapi::upload_slice($srcPath, $bucketName, $dstPath);
        }
        if ($arr['code'] == '0') {//上传成功了
            echo '上传成功';
            echo $arr['data']['access_url'];
            echo '<br>';
            echo $arr['data']['resource_path'];
            //文件已经上传成功,信息已经获取怎么处理就是自己的事了
        } else {
            echo '上传失败<br>';
            //var_dump($arr);
            //exit('上传失败,'.$arr['message']);
            //上传失败的处理方法,一般不会出现才对:)
        }
    }
}
